﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using UserDefinedForms.Entities;
using UserDefinedForms.DAO;

#endregion

namespace UserDefinedForms.DAO.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="FormDataProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class FormDataProviderBaseCore : EntityProviderBase<UserDefinedForms.Entities.FormData, UserDefinedForms.Entities.FormDataKey>
	{		
		#region Get from Many To Many Relationship Functions
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, UserDefinedForms.Entities.FormDataKey key)
		{
			return Delete(transactionManager, key.DataId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_dataId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int32 _dataId)
		{
			return Delete(null, _dataId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_dataId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int32 _dataId);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_DataSupOrder key.
		///		FK_DataSupOrder Description: 
		/// </summary>
		/// <param name="_supOrderCode"></param>
		/// <returns>Returns a typed collection of UserDefinedForms.Entities.FormData objects.</returns>
		public TList<FormData> GetBySupOrderCode(System.Int32? _supOrderCode)
		{
			int count = -1;
			return GetBySupOrderCode(_supOrderCode, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_DataSupOrder key.
		///		FK_DataSupOrder Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_supOrderCode"></param>
		/// <returns>Returns a typed collection of UserDefinedForms.Entities.FormData objects.</returns>
		/// <remarks></remarks>
		public TList<FormData> GetBySupOrderCode(TransactionManager transactionManager, System.Int32? _supOrderCode)
		{
			int count = -1;
			return GetBySupOrderCode(transactionManager, _supOrderCode, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_DataSupOrder key.
		///		FK_DataSupOrder Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_supOrderCode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of UserDefinedForms.Entities.FormData objects.</returns>
		public TList<FormData> GetBySupOrderCode(TransactionManager transactionManager, System.Int32? _supOrderCode, int start, int pageLength)
		{
			int count = -1;
			return GetBySupOrderCode(transactionManager, _supOrderCode, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_DataSupOrder key.
		///		fkDataSupOrder Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_supOrderCode"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of UserDefinedForms.Entities.FormData objects.</returns>
		public TList<FormData> GetBySupOrderCode(System.Int32? _supOrderCode, int start, int pageLength)
		{
			int count =  -1;
			return GetBySupOrderCode(null, _supOrderCode, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_DataSupOrder key.
		///		fkDataSupOrder Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_supOrderCode"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of UserDefinedForms.Entities.FormData objects.</returns>
		public TList<FormData> GetBySupOrderCode(System.Int32? _supOrderCode, int start, int pageLength,out int count)
		{
			return GetBySupOrderCode(null, _supOrderCode, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_DataSupOrder key.
		///		FK_DataSupOrder Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_supOrderCode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of UserDefinedForms.Entities.FormData objects.</returns>
		public abstract TList<FormData> GetBySupOrderCode(TransactionManager transactionManager, System.Int32? _supOrderCode, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override UserDefinedForms.Entities.FormData Get(TransactionManager transactionManager, UserDefinedForms.Entities.FormDataKey key, int start, int pageLength)
		{
			return GetByDataId(transactionManager, key.DataId, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key DataCode index.
		/// </summary>
		/// <param name="_code"></param>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormData&gt;"/> class.</returns>
		public TList<FormData> GetByCode(System.Int32 _code)
		{
			int count = -1;
			return GetByCode(null,_code, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the DataCode index.
		/// </summary>
		/// <param name="_code"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormData&gt;"/> class.</returns>
		public TList<FormData> GetByCode(System.Int32 _code, int start, int pageLength)
		{
			int count = -1;
			return GetByCode(null, _code, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the DataCode index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_code"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormData&gt;"/> class.</returns>
		public TList<FormData> GetByCode(TransactionManager transactionManager, System.Int32 _code)
		{
			int count = -1;
			return GetByCode(transactionManager, _code, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the DataCode index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_code"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormData&gt;"/> class.</returns>
		public TList<FormData> GetByCode(TransactionManager transactionManager, System.Int32 _code, int start, int pageLength)
		{
			int count = -1;
			return GetByCode(transactionManager, _code, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the DataCode index.
		/// </summary>
		/// <param name="_code"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormData&gt;"/> class.</returns>
		public TList<FormData> GetByCode(System.Int32 _code, int start, int pageLength, out int count)
		{
			return GetByCode(null, _code, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the DataCode index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_code"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormData&gt;"/> class.</returns>
		public abstract TList<FormData> GetByCode(TransactionManager transactionManager, System.Int32 _code, int start, int pageLength, out int count);
						
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key DataControl index.
		/// </summary>
		/// <param name="_controlId"></param>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormData&gt;"/> class.</returns>
		public TList<FormData> GetByControlId(System.Int32 _controlId)
		{
			int count = -1;
			return GetByControlId(null,_controlId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the DataControl index.
		/// </summary>
		/// <param name="_controlId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormData&gt;"/> class.</returns>
		public TList<FormData> GetByControlId(System.Int32 _controlId, int start, int pageLength)
		{
			int count = -1;
			return GetByControlId(null, _controlId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the DataControl index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_controlId"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormData&gt;"/> class.</returns>
		public TList<FormData> GetByControlId(TransactionManager transactionManager, System.Int32 _controlId)
		{
			int count = -1;
			return GetByControlId(transactionManager, _controlId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the DataControl index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_controlId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormData&gt;"/> class.</returns>
		public TList<FormData> GetByControlId(TransactionManager transactionManager, System.Int32 _controlId, int start, int pageLength)
		{
			int count = -1;
			return GetByControlId(transactionManager, _controlId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the DataControl index.
		/// </summary>
		/// <param name="_controlId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormData&gt;"/> class.</returns>
		public TList<FormData> GetByControlId(System.Int32 _controlId, int start, int pageLength, out int count)
		{
			return GetByControlId(null, _controlId, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the DataControl index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_controlId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormData&gt;"/> class.</returns>
		public abstract TList<FormData> GetByControlId(TransactionManager transactionManager, System.Int32 _controlId, int start, int pageLength, out int count);
						
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key DataForm index.
		/// </summary>
		/// <param name="_formId"></param>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormData&gt;"/> class.</returns>
		public TList<FormData> GetByFormId(System.Int32 _formId)
		{
			int count = -1;
			return GetByFormId(null,_formId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the DataForm index.
		/// </summary>
		/// <param name="_formId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormData&gt;"/> class.</returns>
		public TList<FormData> GetByFormId(System.Int32 _formId, int start, int pageLength)
		{
			int count = -1;
			return GetByFormId(null, _formId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the DataForm index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_formId"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormData&gt;"/> class.</returns>
		public TList<FormData> GetByFormId(TransactionManager transactionManager, System.Int32 _formId)
		{
			int count = -1;
			return GetByFormId(transactionManager, _formId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the DataForm index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_formId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormData&gt;"/> class.</returns>
		public TList<FormData> GetByFormId(TransactionManager transactionManager, System.Int32 _formId, int start, int pageLength)
		{
			int count = -1;
			return GetByFormId(transactionManager, _formId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the DataForm index.
		/// </summary>
		/// <param name="_formId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormData&gt;"/> class.</returns>
		public TList<FormData> GetByFormId(System.Int32 _formId, int start, int pageLength, out int count)
		{
			return GetByFormId(null, _formId, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the DataForm index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_formId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormData&gt;"/> class.</returns>
		public abstract TList<FormData> GetByFormId(TransactionManager transactionManager, System.Int32 _formId, int start, int pageLength, out int count);
						
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_FormData index.
		/// </summary>
		/// <param name="_dataId"></param>
		/// <returns>Returns an instance of the <see cref="UserDefinedForms.Entities.FormData"/> class.</returns>
		public UserDefinedForms.Entities.FormData GetByDataId(System.Int32 _dataId)
		{
			int count = -1;
			return GetByDataId(null,_dataId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_FormData index.
		/// </summary>
		/// <param name="_dataId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="UserDefinedForms.Entities.FormData"/> class.</returns>
		public UserDefinedForms.Entities.FormData GetByDataId(System.Int32 _dataId, int start, int pageLength)
		{
			int count = -1;
			return GetByDataId(null, _dataId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_FormData index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_dataId"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="UserDefinedForms.Entities.FormData"/> class.</returns>
		public UserDefinedForms.Entities.FormData GetByDataId(TransactionManager transactionManager, System.Int32 _dataId)
		{
			int count = -1;
			return GetByDataId(transactionManager, _dataId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_FormData index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_dataId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="UserDefinedForms.Entities.FormData"/> class.</returns>
		public UserDefinedForms.Entities.FormData GetByDataId(TransactionManager transactionManager, System.Int32 _dataId, int start, int pageLength)
		{
			int count = -1;
			return GetByDataId(transactionManager, _dataId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_FormData index.
		/// </summary>
		/// <param name="_dataId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="UserDefinedForms.Entities.FormData"/> class.</returns>
		public UserDefinedForms.Entities.FormData GetByDataId(System.Int32 _dataId, int start, int pageLength, out int count)
		{
			return GetByDataId(null, _dataId, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_FormData index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_dataId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="UserDefinedForms.Entities.FormData"/> class.</returns>
		public abstract UserDefinedForms.Entities.FormData GetByDataId(TransactionManager transactionManager, System.Int32 _dataId, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;FormData&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;FormData&gt;"/></returns>
		public static TList<FormData> Fill(IDataReader reader, TList<FormData> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				UserDefinedForms.Entities.FormData c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("FormData")
					.Append("|").Append((System.Int32)reader[((int)FormDataColumn.DataId - 1)]).ToString();
					c = EntityManager.LocateOrCreate<FormData>(
					key.ToString(), // EntityTrackingKey
					"FormData",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new UserDefinedForms.Entities.FormData();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.DataId = (System.Int32)reader[((int)FormDataColumn.DataId - 1)];
					c.Code = (System.Int32)reader[((int)FormDataColumn.Code - 1)];
					c.FormId = (System.Int32)reader[((int)FormDataColumn.FormId - 1)];
					c.ControlId = (System.Int32)reader[((int)FormDataColumn.ControlId - 1)];
					c.Data = (System.String)reader[((int)FormDataColumn.Data - 1)];
					c.SupOrderCode = (reader.IsDBNull(((int)FormDataColumn.SupOrderCode - 1)))?null:(System.Int32?)reader[((int)FormDataColumn.SupOrderCode - 1)];
					c.FormType = (System.Int32)reader[((int)FormDataColumn.FormType - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="UserDefinedForms.Entities.FormData"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="UserDefinedForms.Entities.FormData"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, UserDefinedForms.Entities.FormData entity)
		{
			if (!reader.Read()) return;
			
			entity.DataId = (System.Int32)reader[((int)FormDataColumn.DataId - 1)];
			entity.Code = (System.Int32)reader[((int)FormDataColumn.Code - 1)];
			entity.FormId = (System.Int32)reader[((int)FormDataColumn.FormId - 1)];
			entity.ControlId = (System.Int32)reader[((int)FormDataColumn.ControlId - 1)];
			entity.Data = (System.String)reader[((int)FormDataColumn.Data - 1)];
			entity.SupOrderCode = (reader.IsDBNull(((int)FormDataColumn.SupOrderCode - 1)))?null:(System.Int32?)reader[((int)FormDataColumn.SupOrderCode - 1)];
			entity.FormType = (System.Int32)reader[((int)FormDataColumn.FormType - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="UserDefinedForms.Entities.FormData"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="UserDefinedForms.Entities.FormData"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, UserDefinedForms.Entities.FormData entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.DataId = (System.Int32)dataRow["DataID"];
			entity.Code = (System.Int32)dataRow["Code"];
			entity.FormId = (System.Int32)dataRow["FormID"];
			entity.ControlId = (System.Int32)dataRow["ControlID"];
			entity.Data = (System.String)dataRow["Data"];
			entity.SupOrderCode = Convert.IsDBNull(dataRow["SupOrderCode"]) ? null : (System.Int32?)dataRow["SupOrderCode"];
			entity.FormType = (System.Int32)dataRow["FormType"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="UserDefinedForms.Entities.FormData"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">UserDefinedForms.Entities.FormData Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, UserDefinedForms.Entities.FormData entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region CodeSource	
			if (CanDeepLoad(entity, "Order|CodeSource", deepLoadType, innerList) 
				&& entity.CodeSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.Code;
				Order tmpEntity = EntityManager.LocateEntity<Order>(EntityLocator.ConstructKeyFromPkItems(typeof(Order), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.CodeSource = tmpEntity;
				else
					entity.CodeSource = DataRepository.OrderProvider.GetByOrderId(transactionManager, entity.Code);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'CodeSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.CodeSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.OrderProvider.DeepLoad(transactionManager, entity.CodeSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion CodeSource

			#region ControlIdSource	
			if (CanDeepLoad(entity, "FormControl|ControlIdSource", deepLoadType, innerList) 
				&& entity.ControlIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.ControlId;
				FormControl tmpEntity = EntityManager.LocateEntity<FormControl>(EntityLocator.ConstructKeyFromPkItems(typeof(FormControl), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.ControlIdSource = tmpEntity;
				else
					entity.ControlIdSource = DataRepository.FormControlProvider.GetByControlId(transactionManager, entity.ControlId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ControlIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.ControlIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.FormControlProvider.DeepLoad(transactionManager, entity.ControlIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion ControlIdSource

			#region FormIdSource	
			if (CanDeepLoad(entity, "DesignForm|FormIdSource", deepLoadType, innerList) 
				&& entity.FormIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.FormId;
				DesignForm tmpEntity = EntityManager.LocateEntity<DesignForm>(EntityLocator.ConstructKeyFromPkItems(typeof(DesignForm), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.FormIdSource = tmpEntity;
				else
					entity.FormIdSource = DataRepository.DesignFormProvider.GetByFormId(transactionManager, entity.FormId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'FormIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.FormIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.DesignFormProvider.DeepLoad(transactionManager, entity.FormIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion FormIdSource

			#region SupOrderCodeSource	
			if (CanDeepLoad(entity, "Order|SupOrderCodeSource", deepLoadType, innerList) 
				&& entity.SupOrderCodeSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = (entity.SupOrderCode ?? (int)0);
				Order tmpEntity = EntityManager.LocateEntity<Order>(EntityLocator.ConstructKeyFromPkItems(typeof(Order), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.SupOrderCodeSource = tmpEntity;
				else
					entity.SupOrderCodeSource = DataRepository.OrderProvider.GetByOrderId(transactionManager, (entity.SupOrderCode ?? (int)0));		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'SupOrderCodeSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.SupOrderCodeSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.OrderProvider.DeepLoad(transactionManager, entity.SupOrderCodeSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion SupOrderCodeSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the UserDefinedForms.Entities.FormData object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">UserDefinedForms.Entities.FormData instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">UserDefinedForms.Entities.FormData Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, UserDefinedForms.Entities.FormData entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region CodeSource
			if (CanDeepSave(entity, "Order|CodeSource", deepSaveType, innerList) 
				&& entity.CodeSource != null)
			{
				DataRepository.OrderProvider.Save(transactionManager, entity.CodeSource);
				entity.Code = entity.CodeSource.OrderId;
			}
			#endregion 
			
			#region ControlIdSource
			if (CanDeepSave(entity, "FormControl|ControlIdSource", deepSaveType, innerList) 
				&& entity.ControlIdSource != null)
			{
				DataRepository.FormControlProvider.Save(transactionManager, entity.ControlIdSource);
				entity.ControlId = entity.ControlIdSource.ControlId;
			}
			#endregion 
			
			#region FormIdSource
			if (CanDeepSave(entity, "DesignForm|FormIdSource", deepSaveType, innerList) 
				&& entity.FormIdSource != null)
			{
				DataRepository.DesignFormProvider.Save(transactionManager, entity.FormIdSource);
				entity.FormId = entity.FormIdSource.FormId;
			}
			#endregion 
			
			#region SupOrderCodeSource
			if (CanDeepSave(entity, "Order|SupOrderCodeSource", deepSaveType, innerList) 
				&& entity.SupOrderCodeSource != null)
			{
				DataRepository.OrderProvider.Save(transactionManager, entity.SupOrderCodeSource);
				entity.SupOrderCode = entity.SupOrderCodeSource.OrderId;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region FormDataChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>UserDefinedForms.Entities.FormData</c>
	///</summary>
	public enum FormDataChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>Order</c> at CodeSource
		///</summary>
		[ChildEntityType(typeof(Order))]
		Order,
			
		///<summary>
		/// Composite Property for <c>FormControl</c> at ControlIdSource
		///</summary>
		[ChildEntityType(typeof(FormControl))]
		FormControl,
			
		///<summary>
		/// Composite Property for <c>DesignForm</c> at FormIdSource
		///</summary>
		[ChildEntityType(typeof(DesignForm))]
		DesignForm,
		}
	
	#endregion FormDataChildEntityTypes
	
	#region FormDataFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;FormDataColumn&gt;"/> class
	/// that is used exclusively with a <see cref="FormData"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class FormDataFilterBuilder : SqlFilterBuilder<FormDataColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the FormDataFilterBuilder class.
		/// </summary>
		public FormDataFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the FormDataFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public FormDataFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the FormDataFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public FormDataFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion FormDataFilterBuilder
	
	#region FormDataParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;FormDataColumn&gt;"/> class
	/// that is used exclusively with a <see cref="FormData"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class FormDataParameterBuilder : ParameterizedSqlFilterBuilder<FormDataColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the FormDataParameterBuilder class.
		/// </summary>
		public FormDataParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the FormDataParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public FormDataParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the FormDataParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public FormDataParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion FormDataParameterBuilder
	
	#region FormDataSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;FormDataColumn&gt;"/> class
	/// that is used exclusively with a <see cref="FormData"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class FormDataSortBuilder : SqlSortBuilder<FormDataColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the FormDataSqlSortBuilder class.
		/// </summary>
		public FormDataSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion FormDataSortBuilder
	
} // end namespace
